<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="/js/lay-module/frmSelect/formSelects-v4.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>管理员信息（初始密码：123456）</legend>
    </fieldset>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">登录名称</label>
        <div class="layui-input-block">
            <input type="text" name="loginname" id="loginname" lay-verify="required" lay-reqtext="登录名称不能为空" placeholder="登录名称" value="" class="layui-input" disabled>
        </div>
    </div>
    <!-- <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" id="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="密码" value="" class="layui-input" >
        </div>
    </div> -->
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" id="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="用户名" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">角色</label>
        <div class="layui-input-block">
            <select type="text" id="roles" name='roles' placeholder="角色" autocomplete="off" class="layui-input" xm-select="select">
            </select>
        </div>
    </div>
    <div class="layui-form-item" >
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
            <input type="text" id="deptId" name='deptId' placeholder="部门" autocomplete="off" class="layui-input" lay-filter="deptId" />
        </div>
    </div>
    <div class="layui-form-item" pane="">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="true" title="男"" checked="">
            <input type="radio" name="sex" value="false" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">地址</label>
        <div class="layui-input-block">
            <input type="text" name="address" id="address" lay-verify="required" lay-reqtext="地址不能为空" placeholder="地址" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">手机</label>
        <div class="layui-input-block">
            <input type="text" name="phone" id="phone" lay-verify="required|phone" lay-reqtext="手机不能为空" placeholder="手机" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" id="email" lay-verify="required|email" lay-reqtext="邮箱不能为空" placeholder="邮箱" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" pane="">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value='true' title="正常" checked="">
            <input type="radio" name="status" value="false" title="禁用">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-radius layui-btn-primary" lay-submit lay-filter="saveBtn" id="saveBtn">确认保存</button>
            <button class="layui-btn layui-btn-radius layui-btn-primary"  id="backBtn">返回</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js" charset="utf-8"></script>
<script src="../../js/custorm/init.js"></script>
<script src="/js/custorm/MD5.js"></script>
<script>
    layui.use(['form','formSelects'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,formSelects = layui.formSelects;
        HavePermission('adminlist-update')==true?$("#saveBtn").show():$("#saveBtn").hide();
        var user={id:0,username:"",loginname:"",address:"",status:true,deptId:0,sex:1,roles:[],phone:"",email:"",password:''};
        var id=0;
        $(document).ready(function(){
            var content = window.location.href;
            id = content.split('=')[content.split('=').length - 1];
            AxajGet('../../api/sys/GetUserByUserId?id='+id,'../../login.html',initrole)
        });
        form.on('submit(saveBtn)', function (data) {
           // console.log(data.field);
            user=data.field;
            user.id=parseInt(id) ;
            user.deptId=parseInt(data.field.deptId);
            user.sex=data.field.sex=="true"?true:false;
            user.status=data.field.status=="true"?true:false;
            //user.password=md5(user.password);
            AxajPost('../../api/sys/UpdateUser',data.field,'保存成功','../../login.html');
        });

        function initrole(data){
            var arr=new Array();
            var roles = data.roles.split(',');
            for (var n = 0; n < data.allroleid.length; n++) {
                var map = {};
                for (var m = 0; m < roles.length; m++) {
                    if (roles[m]== data.allroleid[n].id ) {
                        map = { "name": data.allroleid[n].name, "value": data.allroleid[n].id, "selected": "selected" };
                        break;
                    } else {
                        map = { "name": data.allroleid[n].name, "value": data.allroleid[n].id };
                    }
                }
                arr.push(map);
            }
            formSelects.data('select', 'local', {
                    arr: arr
            });
            $('#loginname').val(data.loginName); 
            $('#username').val(data.userName); 
            $('#address').val(data.address);
            $('#phone').val(data.phone); 
            $('#email').val(data.email);
            $('#status').val(data.status); 
            $('#sex').val(data.sex);
            $("input[name=sex][value='true']").attr("checked", data.sex == true ? true : false);
            $("input[name=sex][value='false']").attr("checked", data.sex == false ? true : false);

            $("input[name=status][value='true']").attr("checked", data.status == true ? true : false);
            $("input[name=status][value='false']").attr("checked", data.status == false ? true : false);
            form.render();
            // $('#password').val(data.password);
        }
        $("#backBtn").click(function () {
            window.location.href="../user.html";
        });
    });
</script>
</body>
</html>